//
//  ERReaderView.m
//  EpubReader
//
//  Created by zhuxuhong on 2016/12/14.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "ERReaderView.h"

@interface ERReaderViewCell : UICollectionViewCell
<UIWebViewDelegate>

-(void)configureDataWithText: (NSString*)text baseURL: (NSURL*)URL;
@property(nonatomic,copy)UIWebView *webView;

@end

@implementation ERReaderViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.webView];
    }
    return self;
}

-(UIWebView *)webView{
    if (!_webView) {
        UIWebView *web = [UIWebView new];
        web.delegate = self;
        web.scrollView.scrollEnabled = false;
        web.backgroundColor = [UIColor blueColor];
//        web.scrollView.backgroundColor = [UIColor blueColor];
        web.opaque = false;
        
        _webView = web;
    }
    
    return _webView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGRect frame = self.bounds;
    frame.size.width -= 10*2;
    frame.size.height -= 10;
    frame.origin.x = 10;
    frame.origin.y = 10;
    
    _webView.frame = frame;
}

-(void)configureDataWithText: (NSString*)text baseURL: (NSURL*)URL
{
    if (text && URL && text.length>0)
    {
        [_webView loadHTMLString:text baseURL:URL];
    }
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
}

@end




@interface ERReaderView()
<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray *data;

@end

@implementation ERReaderView

-(instancetype)initWithFrame:(CGRect)frame{
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.pagingEnabled = true;
        self.backgroundColor = [UIColor redColor];
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[ERReaderViewCell class] forCellWithReuseIdentifier:@"ERReaderViewCell"];
        _data = [NSMutableArray array];
    }
    
    return self;
}

-(void)setBookText:(NSString *)bookText{
    if (bookText && bookText.length > 0)
    {
        _bookText = bookText;
        
        int i=0;
        while (i < 5) {
            [_data addObject:_bookText];
            i++;
        }
        
        [self reloadData];
    }
}


-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
        return 20;
    }
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return [self itemSize];
}

-(CGSize)screenSize{
    return [UIScreen mainScreen].bounds.size;
}

-(CGSize)itemSize{
    CGSize size = [self screenSize];
    size.height -= 20;
    // 竖屏
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
        size.width /= 2;
    }
    return size;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _data.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ERReaderViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ERReaderViewCell" forIndexPath:indexPath];
    [cell configureDataWithText:_data[indexPath.item] baseURL:[NSURL URLWithString:NSHomeDirectory()]];
    
    return cell;
}


@end
